//
//  XLUserCenterView.m
//  industry
//
//  Created by xl on 2020/11/12.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLUserCenterView.h"
#import "XLUserCenterViewProtocol.h"
#import "XLUserCenterHeaderView.h"
#import "XLUserCenterAdapter.h"
#import "XLUserCenterPresenterProtocol.h"
#import "XLUserCenterInteractorProtocol.h"

@interface XLUserCenterView () <XLUserCenterViewProtocol, XLAdapterDelegate>

@property (nonatomic, strong) XLUserCenterAdapter *adapter;
@property (nonatomic, strong) XLTableView *tableView;
@property (nonatomic, strong) XLUserCenterHeaderView *headerView;

@end
@implementation XLUserCenterView

- (void)dealloc {
    XLLog(@"XLUserCenterView dealloc");
}

#pragma mark - XLUserCenterViewProtocol
- (void)buildView {
    [self addSubview:self.tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.equalTo(self);
    }];
    NSArray *functions = XLSend(self.baseController.presenter, XLUserCenterPresenterProtocol, getFunctions);
    [_adapter setAdapterArray:functions];
    [_tableView reloadData];
    [XLUserData sharedXLUserData].isUserCenterFirst = NO;
    [_headerView refreshTableView:XL_UserInfoData];
    XLSend(self.baseController.presenter, XLUserCenterPresenterProtocol, requestFetchMyAppletList);
}

- (void)refreshTableHeaderView {
    
    if ([XLUserData sharedXLUserData].isUserCenterFirst) {
        [XLUserData sharedXLUserData].isUserCenterFirst = NO;
        
        [_headerView refreshTableView:XL_UserInfoData];
        
        //请求小程序图标
        XLSend(self.baseController.presenter, XLUserCenterPresenterProtocol, requestFetchMyAppletList);
    }
    
   
}

- (void)refreshCurrentCardModel:(id)cardModel archiveModel:(id)archiveModel {
    _adapter.cardModel = cardModel;
    _adapter.archiveModel = archiveModel;
    [_tableView reloadData];
}

#pragma mark - XLAdapterDelegate
- (void)didSelectCellData:(id)cellData {
    XLSend(self.baseController.presenter, XLUserCenterPresenterProtocol, handleCellData:cellData);
}

#pragma mark - Setter/getter
- (XLTableView *)tableView {
    if (!_tableView) {
        _tableView = [[XLTableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor whiteBgColor];
        _tableView.delegate = self.adapter;
        _tableView.dataSource = self.adapter;
        _tableView.tableHeaderView = self.headerView;
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

- (XLUserCenterHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[XLUserCenterHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 12 + SCREEN_WIDTH * 262/375.0)];
    }
    return _headerView;
}

- (XLUserCenterAdapter *)adapter {
    if (!_adapter) {
        _adapter = [[XLUserCenterAdapter alloc] init];
        _adapter.adapterDelegate = self;
    }
    return _adapter;
}

@end
